(function () {

    function formatTime(time) {
        if (typeof time !== 'number') return;
        if (time > 1000) return (time / 1000).toFixed(2) + 's';
        return Math.round(time) + 'ms';
    }

    function resourceType(param) {
        if (/\.(js)/i.test(param)) return 'js';
        if (/\.(css)/i.test(param)) return 'css';
        if (/\.(jpg|jpeg|png|gif|webp|svg)/i.test(param)) return 'image';
        if (/\.(wav|mp3|ogg|acc|webm)/i.test(param)) return 'audio';
        if (/\.(mp4|rmvb|mkv|avi|rm|flv|ogv|webm)/i.test(name)) return 'video';
        return 'other'
    }

    function XFEPerformance() {
        this.timing = {};
        this.otherResouceData = [];
        this.timingFormat = {};
        this.otherResouceDataFormat = {};
        this.otherResouceDataTiming = {
            "js": 0,
            "css": 0,
            "image": 0,
            "audio": 0,
            "video": 0,
            "others": 0
        };
    };

    XFEPerformance.prototype = {
        // 打印数据
        printData: function () {
            // 游览器是否支持
            if (!window.performance) {
                console.log('该浏览器不支持performance接口');
                return;
            }
            // 设置初始数据
            this.timing = window.performance.timing;
            this.otherResouceData = window.performance.getEntriesByType('resource');
            // 当loadEventEnd为0时，重新打印
            var loadTime = this.timing.loadEventEnd - this.timing.navigationStart;
            if (loadTime < 0) {
                setTimeout(this.printData.bind(this), 300);
                return;
            }
            // 设置数据
            this.timingFormat = this.setTimingData(loadTime);
            this.otherResouceDataFormat = this.setOtherData();
            // 打印
            console.table(this.timingFormat);
            for (var key in this.otherResouceDataFormat) {
                console.group(key + " => 加载" + formatTime(this.otherResouceDataTiming[key]));
                console.table(this.otherResouceDataFormat[key]);
                console.groupEnd(key);
            }
        },
        // 设置timing数据
        setTimingData: function (loadTime) {
            var timing = this.timing;
            return {
                "重定向": formatTime(timing.redirectEnd - timing.redirectStart),
                "dns缓存": formatTime(timing.domainLookupStart - timing.fetchStart),
                "dns解析": formatTime(timing.domainLookupEnd - timing.domainLookupStart),
                "tcp连接": formatTime(timing.connectEnd - timing.connectStart),
                "http请求": formatTime(timing.responseStart - timing.requestStart),
                "http响应": formatTime(timing.responseEnd - timing.responseStart),
                "dom解析": formatTime(timing.domContentLoadedEventEnd - timing.responseEnd),
                "load事件": formatTime(timing.loadEventEnd - timing.loadEventStart),
                "页面加载完成": formatTime(loadTime),
                "白屏时间": formatTime(timing.domContentLoadedEventEnd - timing.navigationStart),
            };
        },
        // 设置其他资源数据
        setOtherData: function () {
            var js = [],
                css = [],
                image = [],
                audio = [],
                video = [],
                other = [];
            this.otherResouceData.map(item => {
                var data = {
                    '资源名称': item.name,
                    'HTTP协议': item.nextHopProtocol,
                    "加载时间": formatTime(item.duration)
                };
                switch (resourceType(item.name)) {
                    case 'image':
                        this.otherResouceDataTiming.image += item.duration;
                        image.push(data);
                        break;
                    case 'js':
                        this.otherResouceDataTiming.js += item.duration;
                        js.push(data);
                        break;
                    case 'css':
                        this.otherResouceDataTiming.css += item.duration;
                        css.push(data);
                        break;
                    case 'audio':
                        this.otherResouceDataTiming.audio += item.duration;
                        audio.push(data);
                        break;
                    case 'video':
                        this.otherResouceDataTiming.video += item.duration;
                        video.push(data);
                        break;
                    case 'other':
                        this.otherResouceDataTiming.other += item.duration;
                        other.push(data);
                        break;
                }
            });
            return { js, css, image, audio, video, other };
        }
    };
    chrome.runtime.onMessage.addListener(function () {
        new XFEPerformance().printData();
    });
})()